export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * classList属性在IE9及IE8下的兼容性处理
 */
export function classListPrototypeToIE () {
  if (!('classList' in document.documentElement)) {
    Object.defineProperty(HTMLElement.prototype, 'classList', {
      get: function () {
        var self = this
        function update (fn) {
          return function (value) {
            const classes = self.className.split(/\s+/g)
            const index = classes.indexOf(value)
            fn(classes, index, value)
            self.className = classes.join(' ')
          }
        }
        return {
          add: update(function (classes, index, value) {
            if (!~index) classes.push(value)
          }),
          remove: update(function (classes, index) {
            if (~index) classes.splice(index, 1)
          }),
          toggle: update(function (classes, index, value) {
            if (~index) { classes.splice(index, 1) } else { classes.push(value) }
          }),
          contains: function (value) {
            return !!~self.className.split(/\s+/g).indexOf(value)
          },
          item: function (i) {
            return self.className.split(/\s+/g)[i] || null
          }
        }
      }
    })
  }
}
